import { Component, Input, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { EventService } from 'src/app/shared/service/event.service';
import { MessageService } from 'src/app/shared/service/message.service';
declare var $;
@Component({
  selector: 'app-multiple-select3-filter',
  templateUrl: './multiple-select3-filter.component.html',
  styleUrls: ['./multiple-select3-filter.component.scss'],
})
export class MultipleSelect3FilterComponent implements OnInit {
  @Input() data: any;
  @Input() filterName: any;
  showCheckAll = true;
  defaultActiveKey = '0'; // 默认激活的key
  key = '0';//
  type25Disable = false;
  constructor(
    public modalController: ModalController,
    public appGlobalService: AppGlobalService,
    public eventService: EventService,
    public message: MessageService
  ) { }
  ngOnInit() {

    this.generateListData(this.data);

    // 初始值
    let lastValue: any;
    if (this.data.value && typeof (this.data.value).isArray) {
      lastValue = this.data.value;

    } else if (this.data.value && typeof (this.data.value).isString) {
      lastValue = this.data.value ? this.data.value.split(',') : [];
    } else {
      lastValue = [];
    }
    this.data.value = lastValue;
    const hasOpen = false; // 默认打开第一个有值类
    if (lastValue.length > 0) {
      this.data.children.forEach(element => {

        if (element.children && element.children.length > 0) {
          element.children.forEach(e => {
            if (lastValue.indexOf(e.value) > -1) {
              e.checked = true;
              element.hasValue = true;
              if (!hasOpen) {
                element.open = true;
                this.data.current = element;
              }
            }
            if (e.children && e.children.length > 0) {

              e.children.forEach(i => {
                if (lastValue.indexOf(i.value) > -1) {
                  i.checked = true;
                  element.hasValue = true;
                  if (hasOpen) {
                    element.open = true;
                    this.data.current = element;
                  }
                }
              });
            }
          });
        }
      });
    } else {
      if (this.data && this.data.children && this.data.children.length > 0) {
        // let hasOpen = false; 2023.12.25 fixed bug 
        this.data.children.forEach(element => {
          element.open = false;
        });
        this.data.children[0].open = true;
        this.data.current = this.data.children[0];

      }

    }
    this.checkBigVaue(this.data);
  }

  // 左边大类点击
  itemLeftClick(it, item) {
    item.children.forEach(element => {
      element.open = false;
    });
    it.open = true;
    item.current = it;
    this.checkBigVaue(item);
  }

  unlimitedAll(item) {
    item.current.checked = !item.current.checked;
    // 2023.12.25
    item.current.hasValue = item.current.checked;
    item.current.children.forEach(element => {
      element.checked = item.current.checked;
      element.children.forEach(e => {
        e.checked = item.current.checked;
      });
    });
    // this.checkBigVaue(item);
    console.log('用一个状态表示是否选中了全部=', this.data.current.checked);
    console.log('当前选中的选项卡', this.data.current);
  }

  chooseCurrentAll(item) {
    item.current.checked = !item.current.checked;
    if (item.current.checked) {
      item.current.hasValue = true;
      item.current.children.forEach(element => {
        element.checked = true;
        element.children.forEach(e => {
          e.checked = true;

        });
      });
    } else {
      item.current.hasValue = false;
      item.current.children.forEach(element => {
        element.checked = false;
        element.children.forEach(e => {
          e.checked = false;

        });
      });
    }
    this.checkBigVaue(item);
    console.log(item.value);
    // this.formGroup.patchValue({ [item.formKey]: item.value });
  }

  // 全选
  chooseAll(it, item) {
    it.checked = !it.checked;

    if (it.checked) {
      if (item.current) {
        item.current.hasValue = true;
      }

      if (typeof item.value === 'string') {
        item.value = item.value.split(',');
      }
      item.value.push(it.value);
      // 加入大类 清除小类
      it.children.forEach(element => {
        element.checked = true;
        const index = item.value.findIndex(v => (v === element.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }
      });

      // this.checkBigVaue(item);
    } else {
      if (item.current) {
        item.current.hasValue = false;
      }
      it.children.forEach(element => {
        element.checked = false;
        //  清除 大类、小类
        const index = item.value.findIndex(v => (v === element.value) || it.value === v);
        if (index > -1) {
          item.value.splice(index, 1);
        }
      });

    }
    this.checkBigVaue(item);
    console.log(item.value);
    $('#' + it.value).slideToggle("slow");
    // this.formGroup.patchValue({ [item.formKey]: item.value });


    // this.checkBigVaue(item);
    console.log('用一个状态表示是否选中了全部2=', this.data.current.checked);
  }

  tagClick(tag, it, item) {
    tag.checked = !tag.checked;
    if (typeof item.value === 'string') {
      item.value = item.value.split(',');
      if (item.value.indexOf('') > -1) {
        item.value.splice(item.value.indexOf(''), 1);
      }

    }
    // 没有第三级
    if (tag === it) {
      if (tag.checked) {
        item.current.hasValue = true;

      } else {
        let hasValue = false;
        item.current.children.forEach(element => {
          if (element.checked) {
            hasValue = true;

          }
        });
        item.current.hasValue = hasValue;
      }
    } else {
      let i = 0;
      it.children.forEach(element => {
        // tslint:disable-next-line:no-unused-expression
        element.checked ? ++i : '';
      });
      (i === it.children.length && i > 0) ? it.checked = true : it.checked = false;
      if (it.checked) {
        if (item.current) {
          item.current.hasValue = true;
        }

        // 加入大类 清除小类
        item.value.push(it.value);
        it.children.forEach(element => {

          const index = item.value.findIndex(v => (v === element.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }

        });
      } else {
        it.children.forEach(element => {
          // 先清除
          if (item.current) {
            item.current.hasValue = false;
          }
          if (item.value) {
            const index = item.value.findIndex(v => (v === element.value || v === it.value));
            if (index > -1) {
              item.value.splice(index, 1);
            }
          }


        });

        // 再加入
        it.children.forEach(element => {
          if (element.checked) {
            if (item.current) {
              item.current.hasValue = true;
            }
            item.value.push(element.value);
          }
        });
      }
    }

    this.checkBigVaue(item);
    console.log(item.value);
    // this.formGroup.patchValue({ [item.formKey]: item.value });

  }
  // 检查分类
  checkBigVaue(item) {
    console.log('count:' + new Date().toLocaleDateString(), item);
    // 遍历当前二级分类是否全选
    if (item.current) {
      let n = 0;
      item.current.children.forEach(element => {
        // tslint:disable-next-line:no-unused-expression
        element.checked ? ++n : '';
      });
      // 全选择 清除 二三级
      if (item.current.children.length === n) {
        // TODO 不限 item.current.checked = true;
        item.current.checked = true;
        item.value.push(item.current.value);
        // 清除 二级
        item.current.children.forEach(element => {

          const index = item.value.findIndex(v => (v === element.value));
          if (index > -1) {
            item.value.splice(index, 1);
          }
          // 清除 三级
          this.removeValue(item, element);
        });
      } else {
        item.current.checked = false;
        // 清除一级分类value
        const index = item.value.findIndex(v => (v === item.current.value));
        if (index > -1) {
          item.value.splice(index, 1);
        }
        // 遍历 二 三级
        item.current.children.forEach(element => {
          if (element.checked) {
            item.current.hasValue = true;
            if (item.value.findIndex(v => (v === element.value)) === -1) {
              item.value.push(element.value);
            }

          } else {
            if (item.value.findIndex(v => (v === element.value)) > -1) {
              item.value.splice(item.value.findIndex(v => (v === element.value), 1));
            }
            element.children.forEach(e => {
              if (e.checked) {
                item.current.hasValue = true;
                if (item.value.findIndex(v => (v === e.value)) === -1) {

                  item.value.push(e.value);
                }
              }
            });
          }

        });
      }
    }
  }

  removeValue(item, it) {
    it.children.forEach(e => {
      const index = item.value.findIndex(v => (v === e.value));
      if (index > -1) {
        item.value.splice(index, 1);

      }
    });
  }
  addValue(item, it) {
    it.children.forEach(e => {
      if (e.checked && item.value.findIndex(v => (v === e.value)) === -1) {
        item.value.push(e.value);
      }
    });
  }
  resetClick() {
    this.data.children.forEach(element => {
      element.checked = false;
      element.hasValue = false;
      if (element.children && element.children.length > 0) {
        element.children.forEach(e => {
          e.checked = false;
          if (e.children && e.children.length > 0) {
            e.children.forEach(i => {
              i.checked = false;

            });
          }
        });
      }
    });
  }
  sureClick() {
    // const labelArray: any = [];
    const valueArray: any = [];

    // 2023.12.23 当前选中了全部
    this.data.children.forEach(element => {
      if (element.checked === true) {

        // 2023.12.25 选择了大类则把大类的值传过去
        valueArray .push(element.value);
        // element.children.forEach(e => {
        //   valueArray.push(e.value);
        //   if (e.children && e.children.length > 0) {
        //     e.children.forEach(i => {
        //       valueArray.push(i.value);
        //     });
        //   }
        // });
      } else {
        if (element.children && element.children.length > 0 && element.hasValue) {
          element.children.forEach(e => {
            if (e.checked) {
              valueArray.push(e.value);
            }
            if (e.children && e.children.length > 0) {

              e.children.forEach(i => {
                if (i.checked) {
                  valueArray.push(i.value);

                }

              });
            }
          });
        }
      }


    });



    this.appGlobalService.openBackdrop = false;
    this.eventService.eventEmit.emit('appendResultForSift', { name: this.filterName, value: valueArray.toString() });
    // this.message.sendMessage();
    // 发送查询条件
    // this.modalController.dismiss(valueArray);

  }
  dismissClick() {
    // this.modalController.dismiss();
    this.appGlobalService.openBackdrop = false;
  }


  generateListData(listData) {
    this.data = {
      formType: "modalMultipleSelect3_Auto_Full", // 弹窗自适应完整2-3级分类多选 子项不定宽 (全屏窗)
      label: "提供产品",
      formKey: "pp",
      placeHolder: 'modalMultipleSelect3_Auto_Full',
      value: '',
      tips: "选择的分类的相关商品都将会上架到店铺",
      children: listData,
    };
  }

  //2023.12.23
  accrodionClick(btn, event) { }


}
